﻿#include "MainWidget.h"

void MainWidget::heartFunction()
{
    checkShareMemory++;
    if(checkShareMemory == 10)
    {
        heart.stop();
        //GUN_STATE[i].Page chargingData->gunmsg[i].errcode[0] chargingData->gunmsg[i].meter_state
            //chargingData->can_state[i] chargingData->cmd2[i].result chargingData->gunmsg[i].charge_state
            //chargingData->gunmsg[i].gun_conn_state
        for(int i=0; i<GUN_AMOUNT; i++)
        {
            if(GUN_STATE[i].Page == ChooseGunPage &&
                    chargingData->gunmsg[i].errcode[0] == 0 &&
                    chargingData->gunmsg[i].meter_state == 0 &&
                    chargingData->can_state[i] == 0 &&
                    chargingData->cmd2[i].result == 0 &&
                    chargingData->gunmsg[i].charge_state == 0 &&
                    chargingData->gunmsg[i].gun_conn_state == 0)
            {
                if(i == GUN_AMOUNT -1)
                {
                    system("reboot");
                }
            }
            else
            {
                break;
            }
        }
    }
}

void MainWidget::onUpdateTime()
{
    systemTime = QDateTime::currentDateTime();
    UseWait->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    SystemSetting1->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    SystemSetting2->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    SystemSetting3->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    SystemSetting4->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    ChargeCardManage->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    RecordofCharge->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    FilterRecordofCharge->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    WaringInfomation->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    Carvertify->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    Chargetype->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    SettingofChargeMoney->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    SettingofChargePower->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    SettingofChargeTime->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    Maintainloging->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    Waringinfomation->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    Chargesubscribe->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    ChargewatchingDC->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    ChargewatchingAC->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    Settleaccount->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    ReturnchargeGun->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    EndofChargeVerify->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    ChargeGunnotConnect->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    ChargeGunnotnotConnect->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    CarAndqRCodeVerify->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    Equipmentmaintaince->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    Equipmentupgrade->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    WaitSwipcard->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    Networkinfo->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    Choosegun->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    OutstandingOrder->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    Guninfosetting->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
    Changeguninfo->time->setText(systemTime.toString("yyyy.MM.dd hh:mm:ss"));
}

void MainWidget::onUpdateSignal()
{
//    extern int GetMobileSignal1(int *signal_intensity,char *sys_mode,char *Sim_Operators);

//    int SignalValue = GetMobileSignal();
    int signal_intensity=0;
    char sys_mode=0,Sim_Operators=0;
    GetMobileSignal1(&signal_intensity,&sys_mode,&Sim_Operators);
#ifdef __DEBUG
    qDebug("当前4G信号值为:%d\n", signal_intensity);
    qDebug("网络状态:%d\n", chargingData->net_state);
#endif
    if(chargingData->net_state == 1)//设备在线
    {
        if(line_flag == 0)
        {
            emit On_line();
            line_flag = 1;
        }
        //signal_intensity 0-31   / 99-no serverice
        if(signal_intensity<18)
            pixmapsignal->load(":/Res/image/Signal1.png");
        else if(signal_intensity<20)
            pixmapsignal->load(":/Res/image/Signal2.png");
        else if(signal_intensity<25)
            pixmapsignal->load(":/Res/image/Signal3.png");
        else if(signal_intensity<=31)       // fixed by zhouhao 2021-7-20
            pixmapsignal->load(":/Res/image/Signal4.png");
        else
            pixmapsignal->load(":/Res/image/Signal0.png");
//        switch(signal_intensity/10)
//        {
//        case -9: pixmapsignal->load(":/Res/image/Signal1.png");
//            break;
//        case -7: pixmapsignal->load(":/Res/image/Signal2.png");
//            break;
//        case -6: pixmapsignal->load(":/Res/image/Signal3.png");
//            break;
//        case -5: pixmapsignal->load(":/Res/image/Signal4.png");
//            break;
//        default: pixmapsignal->load(":/Res/image/Signal4.png");
//            break;
//        }

    }
    else if(chargingData->net_state == 0)
    {
        if(line_flag == 1)
        {
            emit Off_line();
            line_flag = 0;
        }
//            pixmapsignal->load(":/Res/image/Signal0.png");
        if(signal_intensity<18)
            pixmapsignal->load(":/Res/image/NoSignal1.png");
        else if(signal_intensity<20)
            pixmapsignal->load(":/Res/image/NoSignal2.png");
        else if(signal_intensity<25)
            pixmapsignal->load(":/Res/image/NoSignal3.png");
        else if(signal_intensity<=31)       // fixed by zhouhao 2021-7-20
            pixmapsignal->load(":/Res/image/NoSignal4.png");
        else
            pixmapsignal->load(":/Res/image/Signal0.png");

//        switch(signal_intensity/10)
//        {
//        case -9: pixmapsignal->load(":/Res/image/NoSignal1.png");
//            break;
//        case -7: pixmapsignal->load(":/Res/image/NoSignal2.png");
//            break;
//        case -6: pixmapsignal->load(":/Res/image/NoSignal3.png");
//            break;
//        case -5: pixmapsignal->load(":/Res/image/NoSignal4.png");
//            break;
//        default: pixmapsignal->load(":/Res/image/Signal0.png");
//            break;

//        }
    }

    UseWait->signal->setPixmap(*pixmapsignal);
    SystemSetting1->signal->setPixmap(*pixmapsignal);
    SystemSetting2->signal->setPixmap(*pixmapsignal);
    SystemSetting3->signal->setPixmap(*pixmapsignal);
    SystemSetting4->signal->setPixmap(*pixmapsignal);
    ChargeCardManage->signal->setPixmap(*pixmapsignal);
    RecordofCharge->signal->setPixmap(*pixmapsignal);
    FilterRecordofCharge->signal->setPixmap(*pixmapsignal);
    WaringInfomation->signal->setPixmap(*pixmapsignal);
    Carvertify->signal->setPixmap(*pixmapsignal);
    Chargetype->signal->setPixmap(*pixmapsignal);
    SettingofChargeMoney->signal->setPixmap(*pixmapsignal);
    SettingofChargePower->signal->setPixmap(*pixmapsignal);
    SettingofChargeTime->signal->setPixmap(*pixmapsignal);
    Maintainloging->signal->setPixmap(*pixmapsignal);
    Waringinfomation->signal->setPixmap(*pixmapsignal);
    Chargesubscribe->signal->setPixmap(*pixmapsignal);
    ChargewatchingDC->signal->setPixmap(*pixmapsignal);
    ChargewatchingAC->signal->setPixmap(*pixmapsignal);
    Settleaccount->signal->setPixmap(*pixmapsignal);
    ReturnchargeGun->signal->setPixmap(*pixmapsignal);
    EndofChargeVerify->signal->setPixmap(*pixmapsignal);
    ChargeGunnotConnect->signal->setPixmap(*pixmapsignal);
    ChargeGunnotnotConnect->signal->setPixmap(*pixmapsignal);
    CarAndqRCodeVerify->signal->setPixmap(*pixmapsignal);
    Equipmentmaintaince->signal->setPixmap(*pixmapsignal);
    Equipmentupgrade->signal->setPixmap(*pixmapsignal);
    WaitSwipcard->signal->setPixmap(*pixmapsignal);
    Networkinfo->signal->setPixmap(*pixmapsignal);
    Choosegun->signal->setPixmap(*pixmapsignal);
    OutstandingOrder->signal->setPixmap(*pixmapsignal);
    Guninfosetting->signal->setPixmap(*pixmapsignal);
    Changeguninfo->signal->setPixmap(*pixmapsignal);
}
void MainWidget::Gun_Showing_timeout()
{
    if(focus_gun_ != 0)
        Gun_Showing.start(400);
    else
        Gun_Showing.stop();
}

void MainWidget::onUpdateDevice()
{
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        if(chargingData->gunmsg[i].gun_conn_state == 0)
            return;
    }

    if(chargingData->update_state == 1 &&
            (stackLayout->currentIndex() == WaitUsePage ||
             stackLayout->currentIndex() == ChooseGunPage))
    {
        stackLayout->setCurrentIndex(equipmentUpgradePage);
    }
    else if(chargingData->update_state == 0 &&
            stackLayout->currentIndex() == equipmentUpgradePage)
    {
        stackLayout->setCurrentIndex(ChooseGunPage);
    }
}

void MainWidget::takeout_guninfo()
{
#ifdef __DEBUG
    qDebug()<<QString("提取信息定时器正在运行--focus_gun_")<<focus_gun_<<" Page--"<<GUN_STATE[focus_gun_-1].Page;
#endif
    if(focus_gun_ != 0)
        stackLayout->setCurrentIndex(GUN_STATE[focus_gun_-1].Page);
    else if(focus_gun_ == 0)
        Gun_Showing.stop();
}
